package cate.game.pvp.teampvp;

import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cate.game.play.part.PlaySide;
import cate.game.pvp.teampvp.dto.*;
import cate.game.rank.po.Rank;
import cate.game.role.Role;
import cate.game.role.pvp.teampvp.msg.TeamPvpMatchResp;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class TeamPvpManager extends ModuleMan {
    public final TeamPvpMsgManager msg = new TeamPvpMsgManager();
    public final TeamPvpRecvManager recv = new TeamPvpRecvManager();
    @Override
    public void initDependent() throws Exception {
        msg.initDependent();
        recv.initDependent();
    }

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        msg.initIndependent(game);
        recv.initIndependent(game);
    }

    private boolean isOpen() {
        return TimeTool.isDayOfWeek(Calendar.SUNDAY)
                || TimeTool.isDayOfWeek(Calendar.FRIDAY)
                || TimeTool.isDayOfWeek(Calendar.SATURDAY);
    }

    private boolean isChallengeOpen() {
        return TimeTool.isDayOfWeek(Calendar.SUNDAY) || TimeTool.isDayOfWeek(Calendar.SATURDAY);
    }

    public SynTeamPvpTeamData syncTeamData(Role role) {
        if (!isOpen()) {
            return null;
        }
        EcResult<PlaySide> psRes = game.fight.side.createPlaySideByRole(role, GDFunc.TEAM_PVP, 1);
        if (!psRes.ok()) {
            return null;
        }
        return msg.syncTeamData(psRes.data);
    }

    public void savePosition(Role role) {
        if (!isOpen()) {
            return;
        }
        EcResult<PlaySide> psRes = game.fight.side.createPlaySideByRole(role, GDFunc.TEAM_PVP, 1);
        if (!psRes.ok()) {
            return;
        }
        TeamPvpMatchData matchData = msg.savePosition(psRes.data);
        if (matchData != null) {
            role.sendNow(new TeamPvpMatchResp(matchData));
        }
    }

    public TeamPvpPlayersData inviteList(Role role) {
        if (!isOpen()) {
            return null;
        }
        return msg.inviteList(role);
    }

    public GameResult inviteApply(Role role, String invitedUid) {
        if (!isOpen()) {
            return null;
        }
        return msg.inviteApply(role, invitedUid);
    }

    public GameResult<TeamPvpTeamData> inviteAnswer(Role role, String leaderUid, boolean ok) {
        if (!isOpen()) {
            return new GameResult<TeamPvpTeamData>().fail("未到开启时间");
        }
        return msg.inviteAnswer(role, leaderUid, ok);
    }

    public GameResult<TeamPvpMatchData> match(Role role) {
        if (!isChallengeOpen()) {
            return new GameResult<TeamPvpMatchData>().fail("未到开启时间");
        }
        return msg.match(role);
    }

    public GameResult<TeamPvpMatchData> matchRefresh(Role role)  {
        if (!isChallengeOpen()) {
            return new GameResult<TeamPvpMatchData>().fail("未到开启时间");
        }
        return msg.matchRefresh(role);
    }

    public void challenge(Role role) {
        if (!isChallengeOpen()) {
            return;
        }
        msg.beginToChallenge(role);
    }

    public TeamPvpMatchData aheadPosition(Role role) {
        if (!isChallengeOpen()) {
            return null;
        }
        return msg.aheadPosition(role.getUid());
    }

    public TeamPvpPlayerSimpleData searchPlayer(String name) {
        if (!isOpen()) {
            return null;
        }
        return msg.searchPlayer(name);
    }

    public GameResult<TeamPvpMatchData> changePosition(Role role, List<String> newUids) {
        if (!isChallengeOpen()) {
            return new GameResult<TeamPvpMatchData>().fail("未到开启时间");
        }
        Set<String> set = new HashSet<>(newUids);
        if (set.size() != 3) {
            return new GameResult<TeamPvpMatchData>().fail("错误的位置");
        }
        return msg.updateFightPosition(role, newUids);
    }

    public List<Rank> loadRanks() {
        return msg.loadRanks();
    }

    public List<Rank> loadGods() {
        return msg.loadGods();
    }

    public TeamPvpInviteApplyData loadInviteApplyList(Role role) {
        return msg.loadInviteApplyList(role.getUid());
    }

    public GameResult<TeamPvpTeamData> changeLeader(Role role, String playerUid) {
        if (StringUtils.isBlank(playerUid)) {
            return new GameResult<TeamPvpTeamData>().fail("请选择正确的转让对象");
        }
        if (StringUtils.equals(role.getUid(), playerUid)) {
            return new GameResult<TeamPvpTeamData>().fail("不能转让给自己哦");
        }
        return msg.changeLeader(role.getUid(), playerUid);
    }

    public GameResult<TeamPvpTeamData> exitTeam(Role role) {
        return msg.exitTeam(role.getUid());
    }

    public GameResult<Void> gmResetChallengeTimes(String playerUid) {
        return msg.gmResetChallengeTimes(playerUid);
    }
}
